package cn.com.callback

import groovy.json.*;
import java.util.Map;

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

import java.text.ParseException
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import cn.com.elite.EliteBean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;

import cn.com.elite.Util;
import cn.com.kh.getProperty;
import cn.com.kh.KHUtil;
/**
 *
 * @Title: BP_job_workorderClose3
 * @Description: 宝安排水工单运营管理平台处理完成后，推送第三方系统结果job
 * @author: chieh
 * @date 2020-08-30 12:30:00
 *
 */
public class BP_job_workorderClose3 implements CallBackInvoke{
	private static final Log log=LogFactory.getLog(BP_job_workorderClose3.class);
	public void doCallBack(EliteBean eliteBean,String callBack,Map map,String paramString){

		log.debug("----------------------------------start BP_job_workorderClose3 from 开始执行任务 -----------------------------");//日志标识，方便查找
		//判断是否有需要推送的任务
		def getworkordernumSelectSQL = eliteBean.chaxun_sql("select count(*) as workordernum from BP_workordertask where status=0 and reqcount<3 and createtime>=GETDATE()-31 and createtime<=GETDATE()","dataSource");
		log.debug("SQL获取数据:"+getworkordernumSelectSQL);
		def workordernum=getworkordernumSelectSQL.get(0).get("workordernum");

		if(workordernum> 0) {
			log.debug("BP_job_workorderClose3开始工单推送第三方结果任务--------------------------");
			String workorderguid =""; //主键
			String business_source =""; //业务来源
			String workorderid =""; //工单编号
			String objective_guid =""; //

			def status=0;
			String json1,injson,returnDealMessage="";
			try{
				def getworkorderSelectSQL = eliteBean.chaxun_sql("select workorderguid,business_source,workorderid,objective_guid from BP_workordertask where status=0 and reqcount<3 and createtime<=GETDATE() ORDER BY reqcount desc","dataSource");
				for(int i=0;i<workordernum;i++) {					
					workorderguid = getworkorderSelectSQL.get(i).get("workorderguid");
					business_source= getworkorderSelectSQL.get(i).get("business_source");
					workorderid= getworkorderSelectSQL.get(i).get("workorderid");
					objective_guid= getworkorderSelectSQL.get(i).get("objective_guid");

					//判断如果是否当前工单为第三方渠道的单
					if(business_source=='CD38FF'||business_source=='9999B1'){
					if(business_source=='CD38FF'){//智慧化网格
						json1="{\"sys_source\":\"WGH\",\"workorderid\":\""+workorderid+"\",\"sys_flag\":\"1\"}";
						Map<String, String> headMap = new HashMap<String, String>();
						headMap.put("Content-Type", "application/json");
						
						def objid;
						def sysurl;
						//判断是否属历史案件
						def objectiveresult = eliteBean.chaxun_sql("select objective_guid as objid from objective where createddate<='2022-02-28 21:00:00.00' and map_id='MYI3EW' and workorderid='" + workorderid + "' and objective_guid ='" + objective_guid + "'", "dataSource");
						if (objectiveresult.size() > 0) {
							objid = objectiveresult.get(0).get("objid");
							log.debug("objid:	" + objid);
							sysurl = "http://127.0.0.1:8080/EliteGeneralHttpTask/GeneralService/BP_WG_apiUnderdelivery";
						}else{
							sysurl = "http://127.0.0.1:8080/EliteGeneralHttpTask/GeneralService/BP_Zhsw_apiUnderdelivery";
						}
						
						injson=Util.doHttpPostUseHead(sysurl,json1,"UTF-8",100000,0,headMap);
						log.debug("injson:	"+injson);
					}else if(business_source=='9999B1'){//数字化城管
						json1="{\"sys_source\":\"CG\",\"workorderid\":\""+workorderid+"\",\"sys_flag\":\"1\"}";
						Map<String, String> headMap = new HashMap<String, String>();
						headMap.put("Content-Type", "application/json");
						def sysurl = "http://127.0.0.1:8080/EliteGeneralHttpTask/GeneralService/BP_CG_apiUnderdelivery";
						injson=Util.doHttpPostUseHead(sysurl,json1,"UTF-8",100000,0,headMap);
					}
					//解析
					log.debug("调用返回信息：" + injson.toString());
					JSONObject inObject = new JSONObject(injson);
					String resultcode = KHUtil.GetJV(inObject, "resultcode");
					log.debug("resultcode：	" + resultcode);
					
					if(resultcode.equals("1")) {
						status=1;
					}else {
						status=0;
					}
					
					String[] batchSQL = new String[1];
					batchSQL[0] = "update BP_workordertask set status='"+status+"',reqcount=reqcount+1,reqparameter='"+json1+"',modifytime=GETDATE(),resparameter='"+injson+"' where workorderguid='"+workorderguid+"' and workorderid='"+workorderid+"'";
					log.debug("更新结果SQL:" + batchSQL.toString());
					def dataresult = eliteBean.updateBatch(batchSQL, "dataSource");
					log.debug("SQL结果:" + dataresult.toString());
					}
				}

			} catch (Exception e) {
				returnDealMessage="接口处理异常" + e.getMessage();
			}
			log.debug(returnDealMessage);
		}
		log.debug("BP_job_workorderClose3当前无工单推送第三方结果发送任务--------------------------");

	}

}